﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using abp.Roles;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;

namespace abp.Users
{
    /// <summary>
    /// 登入用户
    /// </summary>
    public class LoginUser: AuditedAggregateRoot<Guid>
    {
        protected LoginUser()
        {
        }

        public LoginUser(string userName,string displayName,string password)
        {
            UserName = userName;
            DisplayName = displayName;
            Password = password;
        }
        

        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [MaxLength(25)]
        public string UserName { get; set; }

        /// <summary>
        /// 显示名
        /// </summary>
        [MaxLength(50)]
        public string DisplayName
        {
            get
            {
                if (string.IsNullOrEmpty(DisplayName)) return UserName;
                return DisplayName;
            }
            set => DisplayName = value;
        }

        /// <summary>
        /// 密码
        /// </summary>
        [Required]
        [MaxLength(50)]
        public string Password { get; set; }

        /// <summary>
        /// 是否启动
        /// </summary>
        public bool IsEnable { get; set; }


        public  List<LoginRole> Roles { get; set; }

        /// <summary>
        /// 所包含的角色
        /// </summary>
        //public IReadOnlyList<LoginRole> Roles => this.roles?.AsReadOnly();

        /// <summary>
        /// 添加角色
        /// </summary>
        //public virtual void AddRole(LoginRole role)
        //{
        //    //用户添加角色的时候增加校验
        //    if(role==null) throw new UserFriendlyException("角色添加失败,角色是空值");
        //    if (string.IsNullOrEmpty(role.RoleName)) throw new UserFriendlyException("角色添加失败,角色名不能为空");
        //    if (roles != null && roles.Count > 0)
        //    {
        //        if(roles.Exists(r=>r.RoleName == role.RoleName))throw new UserFriendlyException("角色添加失败,当前角色已经存在了");
        //    }

        //    (roles??new List<LoginRole>()).Add(role);
        //}

        //public virtual void RemoveRole(LoginRole role)
        //{
        //    if(role==null) throw new UserFriendlyException("角色移除失败,角色不能为空");
        //    if(roles==null||roles.Count<=0||!roles.Exists(r=>r.RoleName == role.RoleName))
        //        throw new UserFriendlyException("角色移除失败,当前角色已经移除,请刷新重试");
        //    roles.Remove(role);
        //}

        public virtual void SetEnable()
        {
            this.IsEnable = true;
        }


    }
}
